<template>
	<view class="uni-container">
		<view class="content">
			<textarea class="textarea" placeholder="点击按钮获取处理好的域名" v-model="str"></textarea>
			<textarea class="textarea" placeholder="点击按钮获取域名数组" v-model="arr"></textarea>
			<textarea class="textarea" placeholder="向domain.txt中设置数组形式域名" maxlength="-1" v-model="formData.domains"></textarea>
		</view>
		<view class="uni-button-group">
			<button type="primary" class="uni-button" style="width: 100px;" @click="getData">获取域名</button>
			<button type="default" class="uni-button" style="width: 100px;margin-left: 15px;" @click="setData">添加域名</button>
			<navigator open-type="navigateBack" style="margin-left: 15px;">
				<button class="uni-button" style="width: 100px;">返回</button>
			</navigator>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				str: '',
				arr: [],
				formData: {
					domains: []
				}
			}
		},
		methods: {
			getData() {
				let that = this;
				uni.showLoading({
					title: '获取中',
					mask: true
				})
				uniCloud.callFunction({
					name: 'vm-domain',
					data: {
						action: 'getList',
						params: {}
					},
					success: res => {
						that.str = res.result.data.str;
						that.arr = JSON.stringify(res.result.data.arr)
					},
					complete() {
						uni.hideLoading()
					}
				})
			},
			setData() {
				let that = this;
				if (!this.formData.domains.length) return uni.showToast({
					icon: 'none',
					title: '域名数组长度不能为0'
				})
				uni.showModal({
					title: '温馨提示',
					content: '确定向domain.txt中设置这些域名，数组形式？',
					success(res) {
						if (res.confirm) {
							that.setDomain();
						}
					}
				})
			},
			setDomain() {
				let that = this;
				uni.showLoading({
					title: '添加中',
					mask: true
				})
				uniCloud.callFunction({
					name: 'vm-domain',
					data: {
						action: 'writeList',
						params: {
							domains: that.formData.domains
						}
					},
					success: res => {
						switch (res.result.code) {
							case 0:
								// that.arr = JSON.stringify(res.result.data)
								uni.showToast({
									icon: 'success',
									title: `添加成功${res.result.data.length}条`
								})
								break;
							default:
								uni.showModal({
									title: '温馨提示',
									content: `添加失败：${res.result.msg}`,
									showCancel: false
								})
								break;
						}
					},
					complete() {
						uni.hideLoading()
					}
				})
			}
		}
	}
</script>
<style scoped lang="scss">
	.content {
		.textarea {
			width: 100%;
			height: 400rpx;
			padding: 20rpx;
			margin-bottom: 30rpx;
			border-radius: 10rpx;
			background: #f2f5f9;
		}
	}
</style>